package pl.edu.agh.jemo.gui.panels.selection;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.selections.Tournament;

/**
 * Configuration panel allowing to adjust tournament parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing function instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class TournamentConfigPanel extends JPanel {

	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 4727345575901543562L;
	
	/**
	 * Reference to tournament instance configured by panel.
	 */
	protected final Tournament tournament;
	
	/**
	 * Field allowing to set number of specimen returned by tounrament.
	 */
	protected JTextField expectedTournamentReturnText;
	
	/**
	 * Creates Panel instance associated with given tournament reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param tournamentRef reference to tournament, that needs configuration. 
	 */
	public TournamentConfigPanel(Tournament tournamentRef) {
		super();
		tournament = tournamentRef;

		setLayout(null);

		JLabel expectedPopSizeLabel = new JLabel("Expected return population size:");
		expectedPopSizeLabel.setBounds(10, 0, 200, 15);
		add(expectedPopSizeLabel);

		expectedTournamentReturnText = new JTextField(String.valueOf(tournament.getExpectedPopulationSize()));
		expectedTournamentReturnText.setBounds(10, 17, 150, 22);
		add(expectedTournamentReturnText);

		expectedTournamentReturnText.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					tournament.setExpectedPopulationSize(Integer.parseInt(expectedTournamentReturnText
							.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong integer value");
				}
			}
		});
	}
}
